package com.lori.netshop.service.imp;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.druid.support.json.JSONUtils;
import com.lori.netshop.mapper.TbItemParamItemMapper;
import com.lori.netshop.pojo.TbItemParamItem;
import com.lori.netshop.pojo.TbItemParamItemExample;
import com.lori.netshop.pojo.TbItemParamItemExample.Criteria;
import com.lori.netshop.service.ItemParamItemService;
import com.lori.netshop.util.NetshopResult;
@Service
public class ItemParamItemServiceimp implements ItemParamItemService{
	//注入mapper
	@Autowired
	private TbItemParamItemMapper tbItemParamItemMapper;
	@Override
	public NetshopResult save(long item_id, String paramData) {
		TbItemParamItem tbItemParamItem = new TbItemParamItem();
		tbItemParamItem.setItemId(item_id);
		tbItemParamItem.setParamData(paramData);
		tbItemParamItem.setCreated(new Date());
		tbItemParamItem.setUpdated(new Date());
		tbItemParamItemMapper.insert(tbItemParamItem);
		return NetshopResult.ok();
	}
	@Override
	public String find(long item_id) {
		//根据商品id获得数据
		TbItemParamItemExample example = new TbItemParamItemExample();
		Criteria criteria = example.createCriteria();
		criteria.andItemIdEqualTo(item_id);
		List<TbItemParamItem> list = tbItemParamItemMapper.selectByExampleWithBLOBs(example);
		TbItemParamItem tbItemParamItem = list.get(0);
		//将数据转成map格式
		List<Map> parse = (List<Map>) JSONUtils.parse(tbItemParamItem.getParamData());
		//转成html字符串
		StringBuilder sb = new StringBuilder();
		sb.append("<div class=\"Ptable\" style=\"margin-left:600px;font-size:14px;color:#666;\">\n");
		for(Map map:parse){
			sb.append("    <div class=\"Ptable-item\" style=\"width:800px;border-bottom:1px solid #a1a1a1;overflow:auto;\">\n" );
			sb.append("        <h3 style=\"width:200px;float:left;font-size:14px;\">"+map.get("group")+"</h3>\n" );
			sb.append("        <dl style=\"width:550px;float:left;\">\n" );
			List<Map> list2 = (List<Map>) map.get("params");
			for(Map map2:list2){
				sb.append("            <dt style=\"width:100px;float:left;margin-bottom:20px;\">"+map2.get("k")+"</dt><dd style=\"width:400px;float:left;margin-bottom:20px;\">"+map2.get("v")+"</dd>\n" );
			}
			sb.append("        </dl>\n" );
			sb.append("    </div>\n" );
		}
		sb.append("</div>");
		return sb.toString();
	}

}
